package io.renren.modules.zhongyi.controller;

/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.form.RegisterForm;
import io.renren.modules.zhongyi.entity.ZYRegisterUserEntity;
import io.renren.modules.zhongyi.entity.ZYUserEntity;
import io.renren.modules.zhongyi.entity.ZYWeChatUserEntity;
import io.renren.modules.zhongyi.service.ZYUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 注册接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("zy_api")
@Api(tags="注册接口")
public class ZyApiRegisterController {
    @Autowired
    private ZYUserService ZYUserService;

    @PostMapping("wechatRegister")
    @ApiOperation("微信注册")
    public R weChatRegister(@RequestBody ZYRegisterUserEntity map){

        ZYWeChatUserEntity userEntity = new ZYWeChatUserEntity();
        userEntity.setOpenId(map.getOpenId());
        System.out.println("openId---------" + map.getOpenId());

        userEntity.setNickName(map.getNickName());
        userEntity.setAvatarUrl(map.getAvatarUrl());
        userEntity.setProvince(map.getProvince());
        userEntity.setCity(map.getCity());
        userEntity.setGender(map.getGender());
        userEntity.setCreateDate(System.currentTimeMillis()+"");



        ZYUserService.wechatRegisterData(userEntity);


        return null;
    }


    @PostMapping("register")
    @ApiOperation("注册")
    public R register(@RequestBody RegisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        ZYUserEntity user = new ZYUserEntity();
        user.setMobile(form.getMobile());

        user.setUsername(form.getMobile());
        user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        user.setCreateTime(new Date());
        // ZYUserService.save(user);

        return R.ok();
    }
}

